package models

import (
	"order_srv/basic/config"
	"time"
)

// 司机评价
type DriverComment struct {
	Id       int32     `gorm:"column:id;type:int;primaryKey;not null;" json:"id"`
	OrderId  uint32    `gorm:"column:order_id;type:int UNSIGNED;comment:订单id;not null;" json:"order_id"`                           // 订单id
	DriverId uint32    `gorm:"column:driver_id;type:int UNSIGNED;comment:司机id;not null;" json:"driver_id"`                         // 司机id
	UserId   uint32    `gorm:"column:user_id;type:int UNSIGNED;comment:乘客id;not null;" json:"user_id"`                             // 乘客id
	Start    uint8     `gorm:"column:start;type:tinyint UNSIGNED;comment:评级 1-极度不满意 2-不满意 3-一般 4-满意 5-非常棒;not null;" json:"start"` // 评级 1-极度不满意 2-不满意 3-一般 4-满意 5-非常棒
	Comment  string    `gorm:"column:comment;type:varchar(500);comment:评论;not null;" json:"comment"`                               // 评论
	Tags     string    `gorm:"column:tags;type:varchar(255);comment:评价标签，逗号分隔;default:'';" json:"tags"`                         // 评价标签，逗号分隔
	Images   string    `gorm:"column:images;type:text;comment:评价图片URL，逗号分隔;default:'';" json:"images"`                        // 评价图片URL，逗号分隔
	CreateAt time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt time.Time `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (d *DriverComment) TableName() string {
	return "driver_comment"
}

// 创建司机评价
func (d *DriverComment) CreateDriverComment() error {
	return config.DB.Create(&d).Error
}

// 获取司机对指定乘客的评价
func (d *DriverComment) GetDriverCommentByOrder(driverId, orderId uint32) error {
	return config.DB.Where("driver_id = ? AND order_id = ?", driverId, orderId).First(d).Error
}

// 获取乘客收到的所有评价
func GetDriverCommentsByUser(userId uint32) ([]*DriverComment, error) {
	var comments []*DriverComment
	err := config.DB.Where("user_id = ?", userId).Find(&comments).Error
	return comments, err
}
